import axios from 'axios'
const base = '/api/isv'
import ElementUI from 'element-ui'
import $store from "@/store/index"


// 请求数据的公共方法
async function pubServiec(url, params) {
  return new Promise((resolve) => {
    let resData = {
      success: false,
      errorMsg: '',
      data: []
    }
    axios.post(`${base}${url}`, params).then(res => {
      if (res.data.success) {
        resData.success = true;
        resData.data = res.data.result;
      } else {
        resData.errorMsg = res.data.errorMsg || '网络异常';
        ElementUI.Message.warning({
          message: resData.errorMsg,
          type: 'warning'
        })
      }
      resolve(resData);
    }, err => {
      ElementUI.Message.error(`网络异常`);
      resolve(resData);
    });
  });
}


// 汇总数据
async function getDayList(params) {
  $store.state.pageLoading = true;
  let resData = await pubServiec('/infoactivity/day', params);
  $store.state.pageLoading = false;

  if (resData.success) {
    $store.state.showlineChartDataAggregate = false;
    let barList = [];
    $store.state.aggregateDataList = resData.data.day;
    $store.state.aggregateDataInfo = resData.data.list;
    barList.push($store.state.aggregateDataInfo);
    $store.state.lineChartDataAggregate.barData = barList;
    $store.state.showlineChartDataAggregate = true;
  }

  return new Promise((resolve) => {
    resolve(resData);
  });
}
export {
  getDayList
}

// 汇总数据
async function getHourList(params) {
  $store.state.pageLoading = true;
  let resData = await pubServiec('/infoactivity/hour', params);
  $store.state.pageLoading = false

  if (resData.success) {
    let barList = [];
    $store.state.showlineChartDataAggregate = true;
    $store.state.aggregateDataInfo = Object.assign(resData.data);
    barList.push(resData.data);
    $store.state.lineChartDataAggregate.barData = barList;
  }

  return new Promise((resolve) => {
    resolve(resData);
  });
}

export {
  getHourList
}

// 票种分析
async function getSpeciesList(params) {
  $store.state.pageLoading = true;
  let resData = await pubServiec('/infoactivity/ttypeday', params);
  $store.state.pageLoading = false

  if (resData.success) {
    $store.state.showSpeciesList = false;
    $store.state.speciesList = resData.data.day;
    $store.state.lineChartDataSpeciesList.barData = resData.data.list;
    $store.state.showSpeciesList = true;
  }

  return new Promise((resolve) => {
    resolve(resData);
  });
}

export {
  getSpeciesList
}

// 渠道分析
async function channelList(params) {
  $store.state.pageLoading = true;
  let resData = await pubServiec('/infoactivity/chanelday', params);
  $store.state.pageLoading = false

  if (resData.success) {
    $store.state.showChannelList = false;
    $store.state.channelList = resData.data.day;
    $store.state.lineChartDataChannelList.barData = resData.data.list;
    $store.state.showChannelList = true;
  }

  return new Promise((resolve) => {
    resolve(resData);
  });
}

export {
  channelList
}


export const byForumList = params => {
  return axios.post(`${base}/infoticket/queryByForum`, params)
} // 分论坛
